layui.use(['table','layer','form','laydate','tree'], function() {
	var table = layui.table;
	var layer = layui.layer;
	var form = layui.form;
	 var tree = layui.tree;
	var laydate = layui.laydate;
	var $ = layui.jquery;
	
	//新增弹窗中的保存添加事件
		$("body").on("click","#saveBtn",function(){	//二次选择
			//获取form的全部数据
			var data = form.val("roleForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/role/save",data,function(){
				layer.closeAll();
				$("#searchBtn").click();
			});
		});
	
	
	//给新增按钮绑定事件
	$("#addBtn").on("click",function(){		
		//弹出窗口
		layer.open({
			type:1
			,area:['400px','200px']//窗口大小
			,title:"新增角色"
			,content:$("#addRoleTemplate").html()
		});
		
		
		//新增弹窗中的保存添加事件
		/*$("#saveBtn").on("click",function(){	
			//获取form的全部数据
			var data = form.val("userForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/user/save",data,function(){
				layer.closeAll();
				$("#searchBtn").click();
			});
		});*/
		
	});
	
	//给查询按钮绑定事件
	$("#searchBtn").on("click",function(){
		table.render({
			elem: '#roleTable'
			, url: '/role/list' //数据接口
			, page: true //开启分页
			, cols: [[ //表头
				{ field: 'id', title: 'ID', width: '10%', fixed: 'left' }
				, { field: 'name', title: '角色名', width: '20%' }
				, { title: '操作', width: '20%' ,toolbar:"#operateTemplate"}
			]],
			limits:[5,10,15,20],
			limit:5,
			where:{"keyword":$("#searchRolename").val()}
		});
		
		
		
		table.on('tool(roleTable1)',function(obj){
			
			var roleid=obj.data.id;//获取当前操作行的id
			
			if(obj.event == "del" ){//点击删除按钮
				layer.confirm("你确定要删除该角色么？",function(){
					$.post("/role/del",{id:roleid},function(){
						layer.closeAll();
						$("#searchBtn").click();
					});
				}/*,function(){}*/);
			}else if(obj.event == "edit"){//点击编辑按钮
				$.get("/role/"+roleid,function(r){
					layer.open({
						type:1
						,area:['400px','200px']//窗口大小
						,title:"编辑角色"
						,content:$("#addRoleTemplate").html()
					});
					form.val("roleForm",r);//第一个获取值，第二个赋值给u
					/* console.log(r); */
					
					/*$("#saveBtn").on("click",function(){	
						//获取form的全部数据
						var data = form.val("userForm");
						//console.log(data);
						//把数据推送道后台
						//ajax
						$.post("/user/save",data,function(){
							layer.closeAll();
							$("#searchBtn").click();
						});
					});*/
					
				});
			}else{
				$.get("/menu/tree",{roleId:roleid},function(menus){
						
						layer.open({
							type:1
							,title:'绑定角色'
							,area:['400px',"500px"]
							,content:$("#operateTree").html()
						});
						//console.log(menus);
						tree.render({
					      elem: '#MenuTree'  //绑定元素											
					     ,data:menus
						 ,id:"MenuTreeId11"
						,showCheckbox:true
						/*,oncheck:function(obj1){
							console.log(obj1);
						}*/
					    });
						
						$("#saveMenuTree").on("click",function(){
								
							var checkData = tree.getChecked("MenuTreeId11");
							var ids="";
							
						$.each(checkData,function(index,value){
																							
								if(value.children){
									
									$.each(value.children,function(iindex,vvalue){
									if(ids!=""){
									ids+=",";
								}
								ids+=vvalue.id;
								
								});
								
								}
							});
							//console.log(ids);
							$.post("/roleMenu/bind",{roleId:roleid,ids:ids},function(){
							
						});
						});
				form.render();
						
				});
			}
			
		});
		
	}).click();
	
});